﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Windows.Forms;

    public class clsDangKyHoaDon
    {
        private double mDenSo = 0.0;
        private string mKhoaLoaiHoaDonIn = "";
        private string mMauHoaDon = "";
        private string mNgayBDSD = "";
        private int mSoDuDK = 0;
        private string mSoKiHieu = "";
        private double mSoLuong = 0.0;
        private double mTuSo = 0.0;

        public bool CheckThemTruoc(string pMauHoaDon, string pSoKiHieu)
        {
            string commandText = "";
            int num = 0;
            int num2 = 0;
            try
            {
                commandText = "SELECT IsNull(Min(TuSo),0) FROM HT_DangKyHoaDon WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "'";
                num = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
                commandText = "SELECT IsNull(Sum(SoDuDK),0) FROM HT_DangKyHoaDon WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "'";
                num2 = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
                commandText = "SELECT IsNull(GiaTri,0) FROM HT_SoKiHieu WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "'";
                if (Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])) >= num)
                {
                    return false;
                }
                if (num2 > 0)
                {
                    return false;
                }
                if (num <= 1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return false;
        }

        public bool CheckViTriXoa(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu, int pTuSo)
        {
            string commandText = "";
            int num = 0;
            int num2 = 0;
            try
            {
                commandText = "SELECT IsNull(Max(TuSo),0) FROM HT_DangKyHoaDon WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "'";
                num = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
                commandText = "SELECT IsNull(Min(TuSo),0) FROM HT_DangKyHoaDon WHERE Rtrim(MauHoaDon) = '" + pMauHoaDon.Trim() + "' AND Rtrim(SoKiHieu) = '" + pSoKiHieu.Trim() + "'";
                num2 = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
                if ((pTuSo == num2) | (pTuSo == num))
                {
                    return true;
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return false;
        }

        public bool DelData(string pMauHoaDon, string pSoKiHieu, int pTuSo)
        {
            IDataParameter[] commandParameters = new IDataParameter[3];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@MauHoaDon", DbType.Int64, ParameterDirection.Input, pMauHoaDon);
                commandParameters[1] = DBHelper.createParameter("@SoKiHieu", DbType.Int64, ParameterDirection.Input, pSoKiHieu);
                commandParameters[2] = DBHelper.createParameter("@TuSo", DbType.Double, ParameterDirection.Input, pTuSo);
                int num = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.StoredProcedure, "HT_sp_DangKyHoaDon_Del", commandParameters));
                return true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                bool flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return false;
        }

        public int GetDenSo(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu)
        {
            int num;
            string commandText = "";
            try
            {
                commandText = "SELECT isnull(Min(TuSo),0)  FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                num = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.SubtractObject(LVYModule.ReturnIfNull(Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), 0, modGeneral.TypeOfValue.Nothing), 1));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                num = 1;
                ProjectData.ClearProjectError();
                return num;
                ProjectData.ClearProjectError();
            }
            return num;
        }

        public string GetNgayBDSD(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu, string pTask)
        {
            string str;
            string commandText = "";
            try
            {
                if (pTask == "S")
                {
                    commandText = "SELECT IsNull(Max(NgayBDSD),'') FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                }
                else
                {
                    commandText = "SELECT IsNull(Min(NgayBDSD),'') FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                }
                str = Conversions.ToString(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                str = "";
                ProjectData.ClearProjectError();
                return str;
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public int GetSoDuDK(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu, string pTask)
        {
            int num;
            string commandText = "";
            try
            {
                if (pTask == "S")
                {
                    commandText = "SELECT Count(*) FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                }
                else
                {
                    commandText = "SELECT Sum(SoDuDK) FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                }
                num = Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                num = 0;
                ProjectData.ClearProjectError();
                return num;
                ProjectData.ClearProjectError();
            }
            return num;
        }

        public int GetTuSo(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu)
        {
            int num;
            string commandText = "";
            try
            {
                commandText = "SELECT isnull(Max(DenSo),0)  FROM HT_DangKyHoaDon WHERE KhoaLoaiHoaDonIn = '" + pLoaiHoaDon.Trim() + "' and MauHoaDon = '" + pMauHoaDon.Trim() + "' and SoKiHieu = '" + pSoKiHieu.Trim() + "'";
                num = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.AddObject(LVYModule.ReturnIfNull(Conversions.ToInteger(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), 0, modGeneral.TypeOfValue.Nothing), 1));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                num = 1;
                ProjectData.ClearProjectError();
                return num;
                ProjectData.ClearProjectError();
            }
            return num;
        }

        public bool Load()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                string commandText = "SELECT * FROM HT_DangKyHoaDon ORDER BY KhoaLoaiHoaDonIn, MauHoaDon, SoKiHieu, NgayBDSD";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mKhoaLoaiHoaDonIn = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mMauHoaDon = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mSoKiHieu = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mNgayBDSD = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mSoLuong = Conversions.ToDouble(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetDecimal));
                    this.mTuSo = Conversions.ToDouble(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetDecimal));
                    this.mDenSo = Conversions.ToDouble(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoDuDK = Conversions.ToInteger(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetInt32));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save(string PTask)
        {
            bool flag2;
            IDataParameter[] commandParameters = new IDataParameter[9];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@KhoaLoaiHoaDonIn", DbType.Int64, ParameterDirection.Input, this.mKhoaLoaiHoaDonIn);
                commandParameters[1] = DBHelper.createParameter("@MauHoaDon", DbType.Int64, ParameterDirection.Input, this.mMauHoaDon);
                commandParameters[2] = DBHelper.createParameter("@SoKiHieu", DbType.Int64, ParameterDirection.Input, this.mSoKiHieu);
                commandParameters[3] = DBHelper.createParameter("@NgayBDSD", DbType.Int64, ParameterDirection.Input, this.mNgayBDSD);
                commandParameters[4] = DBHelper.createParameter("@SoLuong", DbType.Date, ParameterDirection.Input, this.mSoLuong);
                commandParameters[5] = DBHelper.createParameter("@TuSo", DbType.Date, ParameterDirection.Input, this.mTuSo);
                commandParameters[6] = DBHelper.createParameter("@DenSo", DbType.Date, ParameterDirection.Input, this.mDenSo);
                commandParameters[7] = DBHelper.createParameter("@SoDuDK", DbType.Double, ParameterDirection.Input, this.mSoDuDK);
                commandParameters[8] = DBHelper.createParameter("@Task", DbType.Double, ParameterDirection.Input, PTask);
                bool flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "HT_sp_DangKyHoaDon", commandParameters) > 0;
                flag2 = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag2 = false;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public DataTable ShowList(string pLoaiHoaDon, string pMauHoaDon, string pSoKiHieu)
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            if ((pLoaiHoaDon != "") && (pMauHoaDon != ""))
            {
                str2 = str2 + " AND DK.MauHoaDon = '" + pMauHoaDon + "'";
                if (pSoKiHieu != "")
                {
                    str2 = str2 + " AND DK.SoKiHieu = '" + pSoKiHieu + "'";
                }
            }
            try
            {
                commandText = "SELECT LHDI.Ten as LoaiHoaDonIn, IsNull(KH.GiaTri,0) as SoHienTai, DK.* FROM HT_DangKyHoaDon DK LEFT JOIN HT_LoaiHoaDonIn LHDI ON DK.KhoaLoaiHoaDonIn = LHDI.Khoa LEFT JOIN HT_SoKiHieu KH ON DK.MauHoaDon = KH.MauHoaDon AND DK.SoKiHieu = KH.SoKiHieu WHERE LHDI.Khoa = '" + modGeneral.H_LOAIHOADONINMACDINH + "'" + str2 + " ORDER BY DK.KhoaLoaiHoaDonIn, DK.MauHoaDon, DK.SoKiHieu, DK.NgayBDSD, DK.TuSo";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public double DenSo
        {
            get
            {
                return this.mDenSo;
            }
            set
            {
                this.mDenSo = value;
            }
        }

        public string KhoaLoaiHoaDonIn
        {
            get
            {
                return this.mKhoaLoaiHoaDonIn;
            }
            set
            {
                this.mKhoaLoaiHoaDonIn = value;
            }
        }

        public string MauHoaDon
        {
            get
            {
                return this.mMauHoaDon;
            }
            set
            {
                this.mMauHoaDon = value;
            }
        }

        public string NgayBDSD
        {
            get
            {
                return this.mNgayBDSD;
            }
            set
            {
                this.mNgayBDSD = value;
            }
        }

        public int SoDuDK
        {
            get
            {
                return this.mSoDuDK;
            }
            set
            {
                this.mSoDuDK = value;
            }
        }

        public string SoKiHieu
        {
            get
            {
                return this.mSoKiHieu;
            }
            set
            {
                this.mSoKiHieu = value;
            }
        }

        public double SoLuong
        {
            get
            {
                return this.mSoLuong;
            }
            set
            {
                this.mSoLuong = value;
            }
        }

        public double TuSo
        {
            get
            {
                return this.mTuSo;
            }
            set
            {
                this.mTuSo = value;
            }
        }
    }
}

